package com.xiegang.servicePrice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiegang.internalcommon.constant.CommonStatusEnum;
import com.xiegang.internalcommon.dto.ForecastPriceDto;
import com.xiegang.internalcommon.dto.PriceRule;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.internalcommon.response.DriectionResponse;
import com.xiegang.internalcommon.response.ForecastpriceResponse;
import com.xiegang.internalcommon.util.BigDecimalUtils;
import com.xiegang.servicePrice.mapper.PriceRuleMapper;
import com.xiegang.servicePrice.remote.ServiceMapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author xieGang
 * @description
 * @date 2023/3/12 16:29
 **/
@Service
public class ServicePriceService {

    @Autowired
    private ServiceMapClient serviceMapClient;

    @Autowired
    private PriceRuleMapper priceRuleMapper;

    public ResponseResult forecastPrice(String depLongitude, String deplatitude, String destLongitude, String destLatiude,String cityCode,String vehicleType) {


        ForecastPriceDto forecastPriceDto = new ForecastPriceDto();
        forecastPriceDto.setDepLongitude(depLongitude);
        forecastPriceDto.setDeplatitude(deplatitude);
        forecastPriceDto.setDestLongitude(destLongitude);
        forecastPriceDto.setDestLatiude(destLatiude);

        ResponseResult<DriectionResponse> driving = serviceMapClient.driving(forecastPriceDto);
        Integer distance = driving.getData().getDistance();
        Integer duration = driving.getData().getDuration();

//        Map<String, Object> queryMap = new HashMap<>();
//        queryMap.put("city_code", cityCode);
//        queryMap.put("vehicle_type", vehicleType);
//        List<PriceRule> priceRules = priceRuleMapper.selectByMap(queryMap);

        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", cityCode);
        queryWrapper.eq("vehicle_type", vehicleType);
        queryWrapper.orderByDesc("fare_version");

        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);

        if (priceRules.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getMessage());
        }

        PriceRule priceRule = priceRules.get(0);

        double price = getPrice(distance, duration, priceRule);

        ForecastpriceResponse forecastpriceResponse = new ForecastpriceResponse();
        forecastpriceResponse.setPrice(price);

        return ResponseResult.success(forecastpriceResponse);
    }

    /***
     * @description 根据距离 时长 和 计价规则计算最终价格
     * @author xieGang
     * @date 2023/3/14 13:32
     * @param distance
     * @param duration
     * @param priceRule
     * @return java.lang.Double
     */
    private double getPrice(Integer distance, Integer duration, PriceRule priceRule) {

        double price = 0.0;

        //起步价
        double startFare = priceRule.getStartFare();
        price = BigDecimalUtils.add(price,startFare);

        //里程费
        //总里程 km
        double distanceMile = BigDecimalUtils.divide(distance,1000);
        //起步里程
        double startMile = priceRule.getStartMile();
        double distanceSubtract = BigDecimalUtils.subtract(distanceMile,startMile);

        double mile = distanceSubtract < 0 ? 0 : distanceSubtract;
        //计程单价 元/km
        double unitPricePerMile = priceRule.getUnitPricePerMile();

        //里程价格
        double mileFare = BigDecimalUtils.multiply(mile,unitPricePerMile);
        price = BigDecimalUtils.add(price,mileFare);


        //时长分钟数
        double timeMinutes = BigDecimalUtils.divide(duration,60);
        //时长费
        double unitPricePerMinute = priceRule.getUnitPricePerMinute();
        double timeFare = BigDecimalUtils.multiply(timeMinutes,unitPricePerMinute);

        price = BigDecimalUtils.add(price,timeFare);
        BigDecimal priceDecimal = new BigDecimal(price);
        priceDecimal = priceDecimal.setScale(2,BigDecimal.ROUND_HALF_UP);

        return priceDecimal.doubleValue();
    }

//    public static void main(String[] args) {
//        PriceRule priceRule = new PriceRule();
//        priceRule.setUnitPricePerMile(1.8);
//        priceRule.setUnitPricePerMinute(0.5);
//        priceRule.setStartFare(10.0);
//        priceRule.setStartMile(3);
//        System.out.println(getPrice(6500,1800,priceRule));
//    }

}
